package com.feihua.starter.service.impl;

import com.feihua.starter.service.api.ApiHdbArticlePoService;
import com.feihua.starter.service.api.ApiHdbNewsPoService;
import com.feihua.starter.service.mapper.HdbNewsPoMapper;
import com.feihua.starter.service.modules.hdb.dto.HdbArticleDto;
import com.feihua.starter.service.modules.hdb.dto.HdbNewsDto;
import com.feihua.starter.service.modules.hdb.dto.SearchHdbNewsConditionDto;
import com.feihua.starter.service.modules.hdb.po.HdbNewsPo;
import com.github.pagehelper.Page;
import feihua.jdbc.api.pojo.BasePo;
import feihua.jdbc.api.pojo.PageResultDto;
import feihua.jdbc.api.service.impl.ApiBaseServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * This class was generated by MyBatis Generator.
 *
 * @author revolver 2018-08-30 14:01:21
 */
@Service
public class ApiHdbNewsPoServiceImpl extends ApiBaseServiceImpl<HdbNewsPo, HdbNewsDto, String> implements ApiHdbNewsPoService {
    @Autowired
    private HdbNewsPoMapper hdbNewsPoMapper;
    @Autowired
    private ApiHdbArticlePoService apiHdbArticlePoService;


    public ApiHdbNewsPoServiceImpl() {
        super(HdbNewsDto.class);
    }

    @Override
    public PageResultDto<HdbNewsDto> searchByMultiTable(SearchHdbNewsConditionDto dto, feihua.jdbc.api.pojo.PageAndOrderbyParamDto pageAndOrderbyParamDto) {
        Page p = super.pageAndOrderbyStart(pageAndOrderbyParamDto);
        List<com.feihua.starter.service.modules.hdb.dto.HdbNewsDto> list = hdbNewsPoMapper.searchByMultiTable(dto);
        return new PageResultDto(list, this.wrapPage(p));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public HdbNewsDto addOrUpdate(HdbNewsDto dto) throws Exception {

        updateTop(dto);

        if (dto.getId() == null) {
            add(dto);
        } else {
            update(dto);
        }
        return dto;
    }

    /**
     * 更新
     *
     * @param dto
     *
     * @return
     *
     * @throws Exception
     */
    private HdbNewsDto update(HdbNewsDto dto) throws Exception {

        HdbNewsPo hdbNewsPo = new HdbNewsPo();
        BeanUtils.copyProperties(dto, hdbNewsPo);
        super.preUpdate(hdbNewsPo, dto.getUpdateBy());
        final int i = super.updateByPrimaryKeySelective(hdbNewsPo);
        List<HdbArticleDto> hdbArticles = dto.getHdbArticles();
        if (CollectionUtils.isNotEmpty(hdbArticles)) {
            for (HdbArticleDto hdbArticle : hdbArticles) {
                hdbArticle.setUpdateBy(dto.getUpdateBy());
                hdbArticle.setRelId(hdbNewsPo.getId());
                apiHdbArticlePoService.addOrUpdate(hdbArticle);
            }
        }
        return dto;
    }

    /**
     * 添加资讯
     *
     * @param dto
     *
     * @return
     *
     * @throws Exception
     */
    private HdbNewsDto add(HdbNewsDto dto) throws Exception {
        HdbNewsPo hdbNewsPo = new HdbNewsPo();
        BeanUtils.copyProperties(dto, hdbNewsPo);
        super.preInsert(hdbNewsPo, dto.getCreateBy());
        HdbNewsDto hdbNewsDto = super.insert(hdbNewsPo);
        dto.setId(hdbNewsDto.getId());
        List<HdbArticleDto> hdbArticles = dto.getHdbArticles();
        if (CollectionUtils.isNotEmpty(hdbArticles)) {
            for (HdbArticleDto hdbArticle : hdbArticles) {
                hdbArticle.setCreateBy(dto.getCreateBy());
                hdbArticle.setRelId(hdbNewsDto.getId());
                apiHdbArticlePoService.addOrUpdate(hdbArticle);
            }
        }
        return dto;
    }


    /**
     * 更新置顶
     *
     * @param dto
     */
    private void updateTop(HdbNewsDto dto) {
        if (BasePo.YesNo.Y.name().equals(dto.getTop())) {
            HdbNewsPo condition = new HdbNewsPo();
            condition.setTop(BasePo.YesNo.Y.name());
            HdbNewsPo en = new HdbNewsPo();
            en.setTop(BasePo.YesNo.N.name());
            super.updateSelective(en, condition);
        }
    }
}